package com.wzy.scns.exceptionHandler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandlerController {
    @ExceptionHandler(Exception.class)
    public Map runTimeException(HttpServletResponse response,
          Exception exception)throws IOException{
        System.out.println("Exception");
        Map map = new HashMap();
        map.put("code",0);
        if (exception.getMessage()!=null){
            String msg = exception.getMessage();//得到异常类对象的放回消息
//            System.out.println("msg:"+msg);
            String[] msgs = msg.split(";");//把消息分割出来
//            System.out.println("msgs"+msgs.toString());
            map.put("msg",msgs);
            System.out.println(map);
        }else{
            String[] msgs = null;
            map.put("msg",msgs);
        }
        response.setContentType("text/html; charset=utf-8");
        return map;
    }
}
